#!/usr/bin/python3
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-

import sys
import mrget
import subprocess
import strict_hwcfg
sys.path.append('/usr/lib64/fpemud-os-sysman')
from fm_util import Util
from helper_pkg_warehouse import PkgMerger


pkgName = sys.argv[1]
tmpOp = Util.str01ToBool(sys.argv[2])

mrget.init(system_or_user=True)

if not tmpOp and Util.portageIsPkgInstalled(pkgName):
    print("The specified package is already installed.")
else:
    PkgMerger(strict_hwcfg.probe()).doEmerge("-1 -uDN --with-bdeps=y %s" % (pkgName))
    if not tmpOp:
        # doEmerge() only accepts "-1", so we need to update world file manually
        subprocess.check_call(["emerge", "-q", "--noreplace", pkgName])
